package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencDrill4DocAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Drall2AttachBean;
import patrol.mobile.com.mobilepatrol.bean.Drill2Bean;
import patrol.mobile.com.mobilepatrol.bean.Drill4Bean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.FileUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.HttpDownloader;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyDrill4Activity extends BaseActivity {


    @BindView(R.id.tv_drillUnit)
    TextView tvDrillUnit;
    @BindView(R.id.tv_drillDate)
    TextView tvDrillDate;
    @BindView(R.id.tv_drillPlace)
    TextView tvDrillPlace;
    @BindView(R.id.tv_partakeUnit)
    TextView tvPartakeUnit;
    @BindView(R.id.tv_partakeName)
    TextView tvPartakeName;
    @BindView(R.id.tv_partakeEquip)
    TextView tvPartakeEquip;
    @BindView(R.id.tv_drillDuty)
    EditText tvDrillDuty;
    @BindView(R.id.recyclerViewDoc)
    RecyclerView recyclerViewDoc;


    private EmergencDrill4DocAdapter emergencDrill4DocAdapter;
    private List<Drall2AttachBean> ensureMade2BeanList = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_drill4;
    }

    @Override
    public void setListener() {
        emergencDrill4DocAdapter= new EmergencDrill4DocAdapter(EmergencyDrill4Activity.this);
        requestData(getIntent().getStringExtra("id"));

        emergencDrill4DocAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                final Drall2AttachBean ensureMade2Bean = ensureMade2BeanList.get(position);
                final String url = baseUrl.substring(0, baseUrl.length() - 1) + ensureMade2Bean.getFilePath().replaceAll("\\\\", "/");
                final String path = "ensureMade";
                final String fileName = ensureMade2Bean.getFileName();
                final HttpDownloader httpDownloader = new HttpDownloader();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        httpDownloader.downlaodFile(url, path, fileName);

                    }
                }).start();
                httpDownloader.setDownListener(new HttpDownloader.IDownListener() {
                    @Override
                    public void result(int result) {
                        if (result == -1) {
                            ToastUtils.showToast("下载失败，无法打开");
                            return ;
                        }
                        try {
                            File file = new File(FileUtil.getSDPATH() + path + fileName);
                            Intent intent2 = new Intent("android.intent.action.VIEW");
                            intent2.addCategory("android.intent.category.DEFAULT");
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Uri uri = Uri.fromFile(file);
                            //application/msword
                            //application/vnd.ms-excel
                            if (ensureMade2Bean.getFileType().contains(".docx")) {
                                intent2.setDataAndType(uri, "application/msword");
                            } else if (ensureMade2Bean.getFileType().contains(".xlsx")) {
                                intent2.setDataAndType(uri, "application/vnd.ms-excel");
                            } else {
                                intent2.setDataAndType(uri, "text/plain");
                            }
                            startActivity(intent2);
                        } catch (Exception e) {
                            //没有安装第三方的软件会提示
                            ToastUtils.showToast("没有找到打开该文件的应用程序");
                        }
                    }
                });
            }
        });
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyDrill4Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_DRILL_4);
    }


    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_DRILL_ENTITY);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {


            }

            @Override
            public void onResponse(String code, String response, String attachList) {

                Drill4Bean fileContentBean = GsonUtil.GsonToBean(response, Drill4Bean.class);
                tvDrillUnit.setText(fileContentBean.getDrillUnit());
                tvDrillDate.setText(fileContentBean.getDrillDate());
                tvDrillPlace.setText(fileContentBean.getDrillPlace());
                tvPartakeUnit.setText(fileContentBean.getPartakeUnit());
                tvPartakeName.setText(fileContentBean.getPartakeName());
                tvPartakeEquip.setText(fileContentBean.getPartakeEquip());
                tvDrillDuty.setText(fileContentBean.getDrillDuty());


                ensureMade2BeanList = GsonUtil.jsonToList(attachList, Drall2AttachBean.class);

                emergencDrill4DocAdapter = new EmergencDrill4DocAdapter(EmergencyDrill4Activity.this);
                RecyclerViewHelper.initRecyclerViewV(EmergencyDrill4Activity.this, recyclerViewDoc,
                        emergencDrill4DocAdapter);

                emergencDrill4DocAdapter.updateItems(ensureMade2BeanList);
            }
        });

    }

}
